package easier.app.x.drawable.progress.load;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import androidx.annotation.NonNull;
import easier.app.x.drawable.progress.ProgressDrawable;

public class DotPushDrawable extends ProgressDrawable {

    private float x1;
    private float y1;
    private float y2;

    private float r1;
    private float r2;
    private float r3;
    private float r4;
    private float r5;

    public DotPushDrawable() {

        mPaint.setStyle( Style.FILL );
        mPaint.setColor( Color.RED );
    }

    @Override
    protected void onBoundsChange( Rect bounds ) {

        super.onBoundsChange( bounds );

        float width = bounds.width() * 1f;
        float height = bounds.height() * 1f;

        x1 = width / 14;
        y1 = height / 2;

        r1 = r2 = r3 = r4 = r5 = y1 * 0.5f;
        y2 = y1 * 0.5f;
    }

    @Override
    public void draw( @NonNull Canvas canvas ) {

        canvas.drawCircle( x1, y1, r1, mPaint );
        canvas.drawCircle( x1 * 4, y1, r2, mPaint );
        canvas.drawCircle( x1 * 7, y1, r3, mPaint );
        canvas.drawCircle( x1 * 10, y1, r4, mPaint );
        canvas.drawCircle( x1 * 13, y1, r5, mPaint );
    }

    @Override
    public void onProcessChange( float progress ) {

        float t = 680 * progress;

        if ( t < 200 && t >= 0 ) {
            if ( t <= 100 ) {
                r1 = y2 + y2 * t / 100;
            } else {
                r1 = y1 - y2 * (t - 100) / 100;
            }
        } else {
            r1 = y2;
        }

        t -= 120;

        if ( t < 200 && t >= 0 ) {
            if ( t < 100 ) {
                r2 = y2 + y2 * t / 100;
            } else {
                r2 = y1 - y2 * (t - 100) / 100;
            }
        } else {
            r2 = y2;
        }

        t -= 120;

        if ( t < 200 && t >= 0 ) {
            if ( t < 100 ) {
                r3 = y2 + y2 * t / 100;
            } else {
                r3 = y1 - y2 * (t - 100) / 100;
            }
        } else {
            r3 = y2;
        }

        t -= 120;

        if ( t < 200 && t >= 0 ) {
            if ( t < 100 ) {
                r4 = y2 + y2 * t / 100;
            } else {
                r4 = y1 - y2 * (t - 100) / 100;
            }
        } else {
            r4 = y2;
        }

        t -= 120;

        if ( t < 200 && t >= 0 ) {
            if ( t < 100 ) {
                r5 = y2 + y2 * t / 100;
            } else {
                r5 = y1 - y2 * (t - 100) / 100;
            }
        } else {
            r5 = y2;
        }

        super.onProcessChange( progress );
    }
}
